//
//  GlobalMethod.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//
//

#import "GlobalMethod.h"
#import "BaseNavController.h"
#import "CustomTabBarController.h"
#import "AppDelegate.h"
#import "WXPLoginVC.h"

@implementation GlobalMethod

#pragma mark - delegate执行selector
+ (id)performSelector:(NSString *)selectorName delegate:(id)delegate{
    return [self performSelector:selectorName delegate:delegate object:nil isHasReturn:false];
}
+ (id)performSelector:(NSString *)selectorName delegate:(id)delegate object:(id)object isHasReturn:(BOOL)isHasReturn{
    SEL selector = NSSelectorFromString(selectorName);
    if (delegate != nil && [delegate respondsToSelector:selector]) {
        if (isHasReturn) {
            id result;
            SuppressPerformSelectorLeakWarning(result = [delegate performSelector:selector withObject:object]);
            return result;
        }else{
            SuppressPerformSelectorLeakWarning([delegate performSelector:selector withObject:object]);
            return nil;
        }
    }
    return nil;
}
+ (id)performSelector:(NSString *)selectorName delegate:(id)delegate object:(id)object object:(id)object2 isHasReturn:(BOOL)isHasReturn{
    SEL selector = NSSelectorFromString(selectorName);
    if (delegate != nil && [delegate respondsToSelector:selector]) {
        if (isHasReturn) {
            id result;
            SuppressPerformSelectorLeakWarning(result = [delegate performSelector:selector withObject:object withObject:object2]);
            return result;
        }else{
            SuppressPerformSelectorLeakWarning([delegate performSelector:selector withObject:object withObject:object2]);
            return nil;
        }
    }
    return nil;
}

#pragma mark - 创建rootNav
+ (void)configRootNav{
    BaseNavController * navMain = [[BaseNavController alloc]initWithRootViewController:[CustomTabBarController new]];
    navMain.navigationBarHidden = YES;
    Global_Nav = navMain;
    
    UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
    if (!window) {
        window = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    window.rootViewController = Global_Nav;
    [window setBackgroundColor:[UIColor clearColor]];
    window.backgroundColor = COLOR_WHITE;
    [window makeKeyAndVisible];
    
    AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
    delegate.window = window;
    
    //判断是否需要登陆
    NSLog(@" --- token --- %@",[GlobalData sharedInstance].Global_Token);
    if (!isStr([GlobalData sharedInstance].Global_Token)) {
        [Global_Nav pushViewController:[WXPLoginVC new] animated:false];
    }
}

#pragma mark - 重新登陆
+ (void)relogin{
    [GlobalMethod clearLocalUserInfo];
    if (![Global_Nav.lastVC isKindOfClass:[WXPLoginVC class]]) {
        [Global_Nav pushVCName:@"WXPLoginVC" animated:YES];
    }
}

#pragma mark - 注销-退出登录
+ (void)logoutSuccess{
    [self clearLocalUserInfo];
    [self configRootNav];
}

#pragma mark - 清除全局数据
+ (void)clearLocalUserInfo{
    GlobalData * gbData = [GlobalData sharedInstance];
    gbData.Global_ModelUser = nil;
    gbData.Global_UserCode = nil;
    gbData.Global_Token = nil;
    [self clearUserDefault];
}

@end
